import { AppCTX } from "../../types/koaExtend";
import { createSpokesmanApiMWRouter } from "../../middleware/spokesman-api/brandSpokesmanChecker";
import { getAfterSaleBySerialNumber } from "../../service/afterSaleService";
import { isUndefined } from "mmb";
const router = createSpokesmanApiMWRouter();

// 获取售后单详情(代言人视角)
router.get(
  "/spn/:spokesmanId/afterSale/:afterSaleSerialNumber",
  async (ctx: AppCTX, next) => {
    const { spokesmanId, afterSaleSerialNumber } = ctx.params;
    const { userId } = ctx;

    const afterSale = await getAfterSaleBySerialNumber(afterSaleSerialNumber);
    if (isUndefined(afterSale)) {
      ctx.throw(400, "invalid afterSaleSerialNumber");
    }
    if (
      afterSale.spokesmanId !== spokesmanId ||
      afterSale.spokesmanUserId !== userId
    ) {
      ctx.throw(403, "no permit access to the aftersale");
    }

    ctx.body = afterSale;
  }
);

export default router;
